package com.mj.service;

import com.mj.controller.ChartController;
import com.mj.domain.ActionBean;
import com.mj.domain.ClassificationBean;
import com.mj.domain.RuleBean;
import com.mj.domain.RuleResultBean;
import com.mj.mapper.Mapper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by 杨晓哲 on 2015/12/10.
 */
@Service("dataAccessService")
public class DataAccessService {

    @Autowired
    Mapper mapper;

    private static Log logger = LogFactory.getLog(ChartController.class);

    public List<ActionBean> getAllActions() {
        return mapper.getAllAction();
    }

    public List<ActionBean> getGroupActions(int groupname) {
        return mapper.getGroupActions(groupname);
    }

    public ActionBean getOneAction(int id) {
        return mapper.getOneAction(id);
    }

    public RuleBean getOneRule(int id) {
        return mapper.getOneRule(id);
    }

    public String getNewestResultByRuleId(int ruleId) {
        List<RuleResultBean> newestResult = mapper.getNewestRuleResultByRuleId(ruleId);
        StringBuilder stringBuiler = new StringBuilder();
        for (RuleResultBean ruleResults : newestResult) {
            stringBuiler.append(ruleResults.getResult_list());
        }
        return stringBuiler.toString();
    }

    public List<Integer> getRuleIdsByActionId(int actionId) {
        return mapper.getRuleIdsByActionId(actionId);
    }
//    获取ruleinfo 里面的 json串
    public String queryResultStr(ActionBean actionBean)
    {
//        logger.info("[------service ------]   : queryResultStr");
        List<Integer> ruleIdList = getRuleIdsByActionId(actionBean.getAction_id());
        return getNewestResultByRuleId(ruleIdList.get(0));
    }
//  获取所有的图表所属类别
    public List<ClassificationBean> getAllClassification() {
        logger.info("[------service ------]   : getAllClassification");
        return mapper.getAllClassification();
    }

    public void setOneClassification(ClassificationBean bean) {
//        logger.info("[------service ------]   : setOneClassification");
        mapper.setOneClassification(bean);
    }

    public void setOneAction(ActionBean bean) {
//        logger.info("[------service ------]   : setOneAction");
        mapper.setOneAction(bean);
    }

    public void setOneRule(RuleBean bean) {
//        logger.info("[------service ------]   : setOneRule");
        mapper.setOneRule(bean);
    }

    public int getLastActionId() {
//        logger.info("[------service ------]   : getLastActionId");
        return mapper.getLastActionId();
    }

    public int getLastRuleId() {
//        logger.info("[------service ------]   : getLastRuleId");
        return mapper.getLastRuleId();
    }

    public int getLastSortNum() {
//        logger.info("[------service ------]   : getLastSortNum");
        return mapper.getLastSortNum();
    }

    public int getClassificationIdByName(String name) {
//        logger.info("[------service ------]   : getClassificationIdByName");
        return mapper.getClassificationIdByName(name);
    }
//  创建于一个表格相关的rule 与 action信息
    public void creatOneGraph(ActionBean actionBean,RuleBean ruleBean){
        logger.info("[------service ------]   : creatOneGraph");
//        actionID 与 RuleID 在这里设置并关联
        int maxActionId,maxRuleId;
        int sortNum;

        sortNum = getLastSortNum()+1;
        maxActionId = getLastActionId()+1;
        maxRuleId = getLastRuleId()+1;

        actionBean.setSort_order(sortNum);
        actionBean.setAction_id(maxActionId);
        ruleBean.setRule_id(maxRuleId);
        ruleBean.setAction_id(maxActionId);

        setOneAction(actionBean);
        setOneRule(ruleBean);
    }

    public void updateOneAction(ActionBean actionBean){
        mapper.updateOneAction(actionBean);
    }

    public void updateOneRule(RuleBean ruleBean){
        mapper.updateOneRule(ruleBean);
    }

    public void updateOneGraph(ActionBean actionBean,RuleBean ruleBean){

        logger.info("[------service ------]   : updateOneGraph");

        updateOneAction(actionBean);
        updateOneRule(ruleBean);
    }
}
